#include<bits/stdc++.h>

using namespace std;

int x = 0;
int minn = 0x3f3f3f3f;
string a;
void find(int ff, int len)
{
	for(int i = x - 1; i >= 0; i--)
	{
		if(ff == 0)
		{
			if(a[i] == '0' || a[i] == '5')
			{
				int s = (len - x) + (x - i -1);
				if(s < minn) minn = s;
				return;
			}
		}
		else if(ff == 5)
		{
			if(a[i] == '2' || a[i] == '7')
			{
				int s = (len - x) + (x - i -1);
				//cout<<s;
				if(s < minn) minn = s;
				return;
			}
		}
	}
}
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		minn = 0x3f3f3f3f;
		cin>>a;
		int f;
		int l = a.length() - 1;
		for(int i = l; i >= 0; i--)
		{
			if(a[i] == '0' || a[i] == '5')
			{
				f = a[i] - '0';
				x = i;
				find(f, l);
			}
		}
		cout<<minn<<endl;
	}
	return 0;
}
